//
//  HTFindGoodsDetialVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/30.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTFindGoodsDetialVC.h"
#import "HTDetialMapLineVC.h"
#import "UIImageView+WebCache.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <MapKit/MapKit.h>
#import "HTHolderVC.h"

@interface HTFindGoodsDetialVC ()
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UILabel *endLabel;
@property (weak, nonatomic) IBOutlet UILabel *truckTypeLabel;

@property (weak, nonatomic) IBOutlet UILabel *checkLineLabel;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *detialAddressLabel;

@property (weak, nonatomic) IBOutlet UILabel *remarkLabel;
@property (weak, nonatomic) IBOutlet UILabel *addTypeLabel;


@property (weak, nonatomic) IBOutlet UIImageView *checkimageView;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *ageLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIView *callTapView;
@property (weak, nonatomic) IBOutlet UIView *tapView;

@end

@implementation HTFindGoodsDetialVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"货源详情";
    [self setupInit];
    [self distanceValue];
    
}

- (void)setupInit{
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didPushHolderMain)];
    [self.tapView addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didCheckLine)];
    self.checkimageView.userInteractionEnabled = YES;
    [self.checkimageView addGestureRecognizer:tap];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didCallTap)];
    self.callTapView.userInteractionEnabled = YES;
    [self.callTapView addGestureRecognizer:tap2];
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 20;
    [self reloadData];
    [self netLoadData];
}
- (void)reloadData{
    self.startLabel.text = _goodsSearchModel.fromadd;
    self.endLabel.text = _goodsSearchModel.toadd;
    NSString *length = [HTUtils sortLengthDisplayStringWith:_goodsSearchModel.heiht];
    NSString *type = @"不限车型";
    if (!(_goodsSearchModel.type == nil || _goodsSearchModel.type.length <= 0)) {
        type = _goodsSearchModel.type;
    }
    NSString *dun = @"";
    if ([_goodsSearchModel.utit isEqualToString:@"1"]) {
        dun =[NSString stringWithFormat:@"%@吨",_goodsSearchModel.weight];
    }else{
        dun =[NSString stringWithFormat:@"%@方",_goodsSearchModel.weight];
    }
    NSString *cartype = @"";
    if (!(_goodsSearchModel.cartype == nil || _goodsSearchModel.cartype.length <= 0)) {
        cartype = _goodsSearchModel.cartype;
    }
    self.truckTypeLabel.text = [NSString stringWithFormat:@"%@ %@ / %@ %@ / 运费%@元",length,type,dun,cartype,_goodsSearchModel.pricers];
    self.detialAddressLabel.text =[HTUtils timeIntervalExchange:_goodsSearchModel.createtime];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:_goodsSearchModel.picurl] placeholderImage:[UIImage imageNamed:@"bg_headsculpture"]options:SDWebImageRefreshCached];
    self.nameLabel.text = _goodsSearchModel.name;
    self.ageLabel.text = [NSString stringWithFormat:@"已注册%@",[HTUtils timeIntervalFromLastTime:_goodsSearchModel.registime lastTimeFormat:@"YYYY-MM-dd"]];
    self.numberLabel.text = [NSString stringWithFormat:@"发货%@条",_goodsSearchModel.num];
    self.phoneLabel.text = _goodsSearchModel.phone;
    NSString *time = (_goodsSearchModel.strattime == nil || _goodsSearchModel.strattime.length <= 0)?@"":[NSString stringWithFormat:@"装车时间 %@ ",_goodsSearchModel.strattime];
    NSString *addtype = (_goodsSearchModel.addtype == nil || _goodsSearchModel.addtype.length <= 0)?@"":_goodsSearchModel.addtype;
    NSString *paytype = (_goodsSearchModel.paytype == nil || _goodsSearchModel.paytype.length <= 0)?@"":[NSString stringWithFormat:@"%@ ",_goodsSearchModel.paytype];
    NSString *remark =  (_goodsSearchModel.remark == nil || _goodsSearchModel.remark.length <= 0)?@"":[NSString stringWithFormat:@"%@ ",_goodsSearchModel.remark];
    self.addTypeLabel.text = [NSString stringWithFormat:@"%@%@",time,addtype];
    self.remarkLabel.text = [NSString stringWithFormat:@"%@%@",paytype,remark];
}
- (void)didCallTap{
    NSString *phone = self.goodsSearchModel.phone;
    NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
- (void)didPushHolderMain{
    if (self.goodsSearchModel.cargoid == nil || self.goodsSearchModel.cargoid.length <= 0) {
        [HTShowUtil showMessage:@"货主信息请求失败"];
        return;
    }
    HTHolderVC *holder = [[HTHolderVC alloc]init];
    holder.cargosid = self.goodsSearchModel.cargoid;
    [self.navigationController pushViewController:holder animated:YES];
}
- (void)didCheckLine{
    HTDetialMapLineVC *detial = [[HTDetialMapLineVC alloc]init];
    detial.startName = self.goodsSearchModel.fromadd;
    detial.endName = self.goodsSearchModel.toadd;
    [self.navigationController pushViewController:detial animated:YES];
}
- (void)netLoadData{
    [self startHud];
    [HTHttpRequestManager requestGoodsDetialWithProductid:self.goodsSearchModel.orderid success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"货源信息：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"货源信息请求失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        self.goodsSearchModel = [HTGoodsSearchModel mj_objectWithKeyValues:responseObject[@"data"]];
        
    } failure:^(NSError * _Nonnull error) {
         [self endHud];
    }];
    
}
- (void)setGoodsSearchModel:(HTGoodsSearchModel *)goodsSearchModel{
    _goodsSearchModel = goodsSearchModel;
    [self reloadData];
}
//计算直线距离
- (void)distanceValue{
    
    CLGeocoder *geoC = [[CLGeocoder alloc] init];
    [geoC geocodeAddressString:self.goodsSearchModel.fromadd completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *pl = [placemarks firstObject];
        
        if(error == nil)
        {
            CLLocationCoordinate2D startCoordinate = pl.location.coordinate;
            [geoC geocodeAddressString:self.goodsSearchModel.toadd completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                CLPlacemark *pl = [placemarks firstObject];
                
                if(error == nil)
                {
                    CLLocationCoordinate2D destinationCoordinate = pl.location.coordinate;
                    MKMapPoint point1  = MKMapPointForCoordinate(startCoordinate);
                    MKMapPoint point2  = MKMapPointForCoordinate(destinationCoordinate);
                    CLLocationDistance distance = MKMetersBetweenMapPoints(point1,point2);
                    self.distanceLabel.text = [NSString stringWithFormat:@"%.1f",distance/1000];
                }
            }];
        }
    }];
}
@end
